using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(ContentSizeFitter))]
public class ContentSizeFitterMaxWidth : MonoBehaviour
{
	public float maxWidth;

	private RectTransform _rtfm;

	private ContentSizeFitter _fitter;

	private ILayoutElement _layout;

	private void Awake()
	{
		_rtfm = (RectTransform)base.transform;
		_fitter = GetComponent<ContentSizeFitter>();
		_layout = GetComponent<ILayoutElement>();
	}

	private void Update()
	{
		_fitter.horizontalFit = ((!(_layout.preferredWidth > maxWidth)) ? ContentSizeFitter.FitMode.PreferredSize : ContentSizeFitter.FitMode.Unconstrained);
		if (_layout.preferredWidth > maxWidth)
		{
			_fitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
			_rtfm.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, maxWidth);
		}
		else
		{
			_fitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
		}
	}
}
